<?php

/**
 * 每天定时计算 品牌前台分类 商品占比  算出前台分类品牌指数
 */

ini_set('display_errors', 1);
ini_set('memory_limit', '1024M');
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShopCategoryClassify as mShopCategoryClassify;
use Biz\model\wiki\WikiBasic as mWikiBasic;
use Biz\cache\brand\BrandScoreCache;
use Doraemon\cache\Wiki\SearchBrandScore;

$shopCategory = mShopCategoryClassify::selectInfos(['deep' => 2, 'status' => mShopCategoryClassify::STATUS_NORMAL], '*', false);
$shopCategory = array_column($shopCategory, null, 'id');
$ids = array_keys($shopCategory);
$list = mShopCategoryClassify::selectInfos(['parent_id' => $ids, 'status' => mShopCategoryClassify::STATUS_NORMAL, 'cate_id[>]' => 0], '*', false);

$firstClassifyArr = [];
foreach ($shopCategory as $classifyId => $row) {
    $firstClassifyArr[$row['parent_id']][] = $row['id'];
}

$cateIdsForClassify = [];
foreach ($list as $row) {
    $cateIdsForClassify[$row['cate_id']][] = $shopCategory[$row['parent_id']];
}

$where = [
    'basic_state' => mWikiBasic::STATE_ONLINE,
    'brand_id[>]' => 0,
];
$basicList = mWikiBasic::selectByWhere($where);
$basicBrandWikiList = [];
foreach ($basicList as $row) {
    $basicBrandWikiList[$row['brand_id']][] = $row;
}
$brandClassfiyBasicList = [];
foreach ($basicBrandWikiList as $brandId => $rowList) {
    foreach ($rowList as $basicRow) {
        if ($basicRow['category_id'] && isset($cateIdsForClassify[$basicRow['category_id']])) {
            foreach ($cateIdsForClassify[$basicRow['category_id']] as $classifyRow) {
                //$brandClassfiyBasicList[$brandId][$classifyRow['classify_name']][] = $basicRow['id'];
                $brandClassfiyBasicList[$brandId][$classifyRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['second_category_id'] && isset($cateIdsForClassify[$basicRow['second_category_id']])) {
            foreach ($cateIdsForClassify[$basicRow['second_category_id']] as $classifyRow) {
                //$brandClassfiyBasicList[$brandId][$classifyRow['classify_name']][] = $basicRow['id'];
                $brandClassfiyBasicList[$brandId][$classifyRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['third_category_id'] && isset($cateIdsForClassify[$basicRow['third_category_id']])) {
            foreach ($cateIdsForClassify[$basicRow['third_category_id']] as $classifyRow) {
                //$brandClassfiyBasicList[$brandId][$classifyRow['classify_name']][] = $basicRow['id'];
                $brandClassfiyBasicList[$brandId][$classifyRow['id']][] = $basicRow['id'];
            }
        } else if ($basicRow['four_category_id'] && isset($cateIdsForClassify[$basicRow['four_category_id']])) {
            foreach ($cateIdsForClassify[$basicRow['four_category_id']] as $classifyRow) {
                //$brandClassfiyBasicList[$brandId][$classifyRow['classify_name']][] = $basicRow['id'];
                $brandClassfiyBasicList[$brandId][$classifyRow['id']][] = $basicRow['id'];
            }
        }
    }
}

SearchBrandScore::delItem(SearchBrandScore::CATEGORY_BRAND_SCORE);
$result = [];
foreach ($brandClassfiyBasicList as $brandId => $rows) {
    $brandWikiCount = count($basicBrandWikiList[$brandId]);
    $brandScore = BrandScoreCache::getBrandScore($brandId);
    $brandScore = empty($brandScore) ? 1 : $brandScore;
    var_dump('brand Id:' . $brandId . '的品牌指数：' . $brandScore);
    SearchBrandScore::delClassifyBrandScore($brandId);
    foreach ($rows as $classifyId => $basicIds) {
        if (isset($shopCategory[$classifyId])) {
            $classfiyName = $shopCategory[$classifyId]['classify_name'];
            $basicIds = array_unique(array_filter($basicIds));
            $scale = (count($basicIds) / $brandWikiCount);//商品占比
            $scale = $brandScore * (1 + $scale);
            var_dump('分类品牌指数：' . $brandId . '_' . $classfiyName . "(" . $classifyId . "):" . $scale);
            SearchBrandScore::setCatBrandScoreById($brandId . '_' . $classfiyName, $scale);
            SearchBrandScore::setClassifyBrandScore($brandId, $classifyId, $scale);
        }
    }
    $brandAllScoreArr = SearchBrandScore::getAllClassifyBrandScore($brandId);
    $i = 0;
    foreach ($firstClassifyArr as $firstId => $secondIds) {
        $firstScore = 0;
        foreach ($secondIds as $secondId) {
            $val = isset($brandAllScoreArr[$secondId]) ? $brandAllScoreArr[$secondId] : 0;
            $firstScore += $val;
        }
        var_dump('一级分类品牌指数：' . $brandId . '#' . $firstId . '#' . $firstScore);
        SearchBrandScore::setClassifyBrandScore($brandId, $firstId, $firstScore);
        if ($firstScore) {
            SearchBrandScore::addClassifyBrands($firstId, [$brandId => $firstScore]);
        }
    }


}


